class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        int sum=0;
        unordered_map<int,int> has;
        has[0]=0;
        int ret=0;
        for(int i=0;i<nums.size();i++)
        {
            sum+=nums[i]==0?-1:1;
            if(has.count(sum))
            {
                ret=max(ret,i+1-has[sum]);
            }
            else{
                has[sum]=i+1;
            }
        }
        return ret;
    }
};
